
#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"

struct Yuce{
    std::vector<int> pm;
    int right;
};
class OneLoop{
public:
    std::vector<Yuce> yuces;
    std::vector<int> visited;
    std::vector<char> vis;
    std::list<std::vector<int>> oks;
    bool pm_ok(std::vector<int> &pm){
        int j=0;
        for(int i=0;i<pm.size();i++){
            while(j<visited.size()&&visited[j]!=pm[i]){
                j++;
            }
            if(j==visited.size()){
                //如果找到了末尾还是没找到，那么不匹配
                return false;
            }
        }
        return true;
    }
    void check_ok(){
        for(auto &yuce:yuces){
            //预测是错的，但是符合排名，说明当前排名不对
            if(pm_ok(yuce.pm)&&!yuce.right){
                return;
            }
            //预测是对的，但不符合排名，说明排名不对
            if(!pm_ok(yuce.pm)&&yuce.right){
                return;
            }
        }
        oks.push_back(visited);
    }
    void dfs(int depth){
        if(depth==visited.size()){
            //检测是否匹配预测
            check_ok();
            return;
        }
        for(int i=0;i<visited.size();i++){
            if(vis[i]==0){
                vis[i]=1;
                visited[depth]=i;
                dfs(depth+1);
                vis[i]=0;
            }
        }
    }
    void run(
            int a,int b
            ){
        visited.resize(a,-1);
        vis.resize(a);
        for(int i=0;i<b;i++){
            Yuce yc;
            int in;std::cin>>in;//每行第一个数
            yc.pm.resize(in);
            for(int j=0;j<in;j++){
                int rs;std::cin>>rs;
                yc.pm[j]=rs;
            }
            std::cin>>yc.right;

            yuces.push_back(yc);
        }

        dfs(0);
        printf("%d\n",oks.size());
        for(auto &ok:oks){
            for(auto hh:ok){
                printf("%d ",hh);
            }
            printf("\n");
        }
    }
};
class Solution {
public:
    void run() {

        bool first = true;
        int a,b,c,d,e;
//        std::string s1,s2;
        while (
                std::cin>>a>>b
//                std::getline(std::cin,s1)
                )
        {
//            std::getline(std::cin,s2);
            OneLoop loop;
            loop.run(a,b);
        }
    }
};

int main() {
//    bool cur_num= true;
    Solution().run();
    return 0;
}

